/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.listener;

import com.blandware.atleap.common.Constants;
import com.blandware.atleap.service.core.StatisticsManager;
import com.blandware.atleap.webapp.util.core.GlobalProperties;
import com.blandware.atleap.webapp.util.core.RawDataCollector;
import com.blandware.atleap.webapp.util.core.StatisticsUtil;
import com.blandware.atleap.webapp.util.core.WebappConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * <p>Listens for session destroy event; when it's occured, raw statistics data
 * is flushed.</p>
 * <p><a href="FlushRawDataListener.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.4 $ $Date: 2007/12/21 13:41:11 $
 * @web.listener
 */
public class FlushRawDataListener implements HttpSessionListener {

    /**
     * Commons logging instance
     */
    protected transient final Log log = LogFactory.getLog(FlushRawDataListener.class);

    /**
     * Processes session creation.
     *
     * @param event event
     */
    public void sessionCreated(HttpSessionEvent event) {
    }

    /**
     * Processes session destroy.
     *
     * @param event event
     */
    public void sessionDestroyed(HttpSessionEvent event) {
        HttpSession session = event.getSession();
        ServletContext servletContext = session.getServletContext();

        GlobalProperties globalProperties = GlobalProperties.getInstance(servletContext);
        if (globalProperties.getBoolean(WebappConstants.STATISTICS_ENABLED_KEY, true).booleanValue()) {
            ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
            StatisticsManager statisticsManager = (StatisticsManager) ctx.getBean(Constants.STATISTICS_MANAGER_BEAN);
            StatisticsUtil statisticsUtil = StatisticsUtil.getInstance(servletContext);

            RawDataCollector collector = RawDataCollector.getInstance(session, null, statisticsUtil, null);
            collector.flushData(statisticsManager, true);
        }
    }
}
